{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Jigsaw for Python"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = 42"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"application/javascript": [
"\n",
" if (document.jigsaw_register_workspace === undefined) {\n",
"\n",
" document.jigsaw_workspaces = {};\n",
"\n",
" document.jigsaw_register_workspace = function(workspace_filename, workspace, xml_init) {\n",
" workspace.element = document.element;\n",
" document.jigsaw_workspaces[workspace_filename] = workspace;\n",
"\n",
" try {\n",
" $([window.parent.IPython.events]).on('notebook_saved.Notebook', function() { \n",
" try {\n",
" document.jigsaw_save_workspace(workspace_filename); \n",
" } catch(err) {\n",
" // ignore failure, might not exist\n",
" }\n",
" });\n",
" } catch (err) {\n",
" // rendering for display\n",
" }\n",
" \n",
" var xml = document.jigsaw_loadXMLDoc(workspace_filename);\n",
" if (xml === null) {\n",
" xml = xml_init;\n",
" if (xml === null) {\n",
" xml = Blockly.Xml.textToDom('');\n",
" }\n",
" } else {\n",
" xml = xml.children[0]; // document\n",
" }\n",
" Blockly.Xml.domToWorkspace(workspace, xml);\n",
" };\n",
"\n",
" document.jigsaw_handle_output = function(workspace_filename, out) {\n",
" var workspace = document.jigsaw_workspaces[workspace_filename];\n",
" //var output_area = workspace.element.output_area;\n",
" var cell_index = document.jigsaw_get_cell(workspace.element);\n",
" var cell = IPython.notebook.get_cell(cell_index);\n",
" var res = null;\n",
" var data = null;\n",
" document.cell = cell;\n",
" document.out = out;\n",
" if (out.msg_type == \"stream\") {\n",
" res = out.content.text;\n",
" //document.getElementById('code_output').value += res.toString();\n",
" } else if (out.msg_type === \"pyout\") {\n",
" // if output is a python object\n",
" res = out.content.data[\"text/plain\"];\n",
" //document.getElementById('code_output').value += res.toString(); \n",
" } else if (out.msg_type == \"pyerr\") {\n",
" // if output is a python error\n",
" res = out.content.data[\"text/plain\"];\n",
" //document.getElementById('code_output').value += res.toString();\n",
" } else if (out.msg_type == \"execute_result\") {\n",
" var str = out.content.data[\"text/plain\"];\n",
" res = str;\n",
" if (res.indexOf(\"u\") == 0)\n",
" res = res.substring(2, res.length - 1) + \"\\n\";\n",
" if (res) {\n",
" //document.getElementById('code_output').value += res.toString();\n",
" }\n",
" } else if (out.msg_type == \"error\") {\n",
" res = out.content.ename + \": \" + out.content.evalue;\n",
" // FIXME: out.traceback is Array of terminal color-coded [-codes\n",
" } else {\n",
" // if output is something we haven't thought of\n",
" res = out.toString();\n",
" //document.getElementById('code_output').value += res.toString();\n",
" }\n",
" if (res) {\n",
" cell.output_area.append_output({output_type: \"stream\", text: res.toString(), name: \"output\"});\n",
" }\n",
" };\n",
" \n",
" document.jigsaw_generate = function(workspace_filename, language, insert_code) {\n",
" var workspace = document.jigsaw_workspaces[workspace_filename];\n",
" var callbacks = { 'iopub' : {'output' : function(out) { document.jigsaw_handle_output(workspace_filename, out); }}};\n",
" var code = Blockly[language].workspaceToCode(workspace);\n",
" if (insert_code == 1) {\n",
" var cell_index = document.jigsaw_get_cell(workspace.element);\n",
" var cell = IPython.notebook.insert_cell_at_index(0, cell_index + 1);\n",
" cell.set_text(code);\n",
" } else {\n",
" window.parent.IPython.notebook.kernel.execute(code,\n",
" callbacks,\n",
" {silent: false});\n",
" }\n",
" };\n",
" \n",
" document.jigsaw_save_workspace = function(workspace_filename) {\n",
" var workspace = document.jigsaw_workspaces[workspace_filename];\n",
" var xml = Blockly.Xml.workspaceToDom(workspace);\n",
" document.xml = xml;\n",
" if (xml !== undefined) {\n",
" console.log(xml);\n",
" //xml.style = \"display: none\";\n",
" //xml.id = \"workspace\";\n",
" var xml_text = Blockly.Xml.domToText(xml)\n",
" IPython.notebook.kernel.execute('%%file ' + workspace_filename + '\\n' + xml_text);\n",
" }\n",
" };\n",
" \n",
" document.jigsaw_loadXMLDoc = function(filename) {\n",
" var xhttp = new XMLHttpRequest();\n",
" xhttp.open(\"GET\", filename, false);\n",
" xhttp.send();\n",
" return xhttp.responseXML;\n",
" };\n",
" }\n",
"\n",
" document.jigsaw_get_cell = function (element) {\n",
" // FIXME: brittle and ugly:\n",
" var mydiv = element[0].parentNode.parentNode.parentNode.parentNode;\n",
" var cells = IPython.notebook.get_cells();\n",
" for (var i = 0; i < cells.length; i++) {\n",
" if (mydiv === cells[i].element[0]) {\n",
" return i;\n",
" }\n",
" }\n",
" return null;\n",
" };\n",
"\n",
" document.jigsaw_clear_output = function (workspace_filename) {\n",
" var workspace = document.jigsaw_workspaces[workspace_filename];\n",
" var cell_index = document.jigsaw_get_cell(workspace.element);\n",
" var cell = IPython.notebook.get_cell(cell_index);\n",
" // FIXME: brittle and ugly:\n",
" cell.element[0].children[2].children[1].children[2].children[1].children[0].innerHTML = \"\"\n",
" cell.output_area.outputs[2].text = \"\"\n",
" };\n",
"\n",
" try {\n",
" document.element = element;\n",
" } catch (err) {\n",
" // rendering\n",
" }\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "output",
"output_type": "stream",
"text": [
"42\n"
]
}
],
"source": [
"%jigsaw Python --workspace workspace2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def main():\n",
" global x, y\n",
" print(x)\n",
" y = x * 2\n",
"\n",
"\n",
"main()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def main():\n",
" global x, y\n",
" print(x)\n",
" y = x * 2\n",
"\n",
"\n",
"main()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}